package com.yntx.usercenter.authorization.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yntx.usercenter.authorization.dto.PermissionDTO;
import com.yntx.usercenter.authorization.converter.PermissionConverter;
import com.yntx.usercenter.authorization.dto.PermissionDeleteDTO;
import com.yntx.usercenter.authorization.dto.PermissionUpdateDTO;
import com.yntx.usercenter.authorization.mapper.PermissionMapper;
import com.yntx.usercenter.authorization.mapper.RolePermissionMapper;
import com.yntx.usercenter.authorization.po.Permission;
import com.yntx.usercenter.authorization.po.RolePermission;
import com.yntx.usercenter.authorization.service.PermissionService;
import com.yntx.usercenter.authorization.vo.PermissionVO;
import lombok.AllArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 许可服务impl
 *
 * @author wuzhengyu
 * @date 2023/05/09 21:49
 */
@Service
@AllArgsConstructor
public class PermissionServiceImpl implements PermissionService {

    private PermissionMapper permissionMapper;

    private RolePermissionMapper rolePermissionMapper;

    private static final PermissionConverter CONVERTER = PermissionConverter.INSTANCE;

    @Override
    public List<PermissionVO> listByRole(Long permissionId) {
        LambdaQueryWrapper<RolePermission> permissionPermissionQuery = new LambdaQueryWrapper<RolePermission>().eq(RolePermission::getPermissionId, permissionId);
        List<RolePermission> permissionPermissionList = rolePermissionMapper.selectList(permissionPermissionQuery);
        if (CollectionUtils.isEmpty(permissionPermissionList)) {
            return Collections.emptyList();
        }
        LambdaQueryWrapper<Permission> permissionQuery = new LambdaQueryWrapper<Permission>().in(Permission::getId, permissionPermissionList.stream().map(RolePermission::getId).collect(Collectors.toSet()));
        List<Permission> permissionList = permissionMapper.selectList(permissionQuery);
        return CONVERTER.permission2PermissionVoList(permissionList);
    }

    @Override
    public List<PermissionVO> list(PermissionDTO permissionDto) {
        LambdaQueryWrapper<Permission> permissionQuery = new LambdaQueryWrapper<Permission>()
                .eq(StringUtils.isNotEmpty(permissionDto.getCode()), Permission::getCode, permissionDto.getCode())
                .like(StringUtils.isNotEmpty(permissionDto.getName()), Permission::getName, permissionDto.getName());
        List<Permission> permissionList = permissionMapper.selectList(permissionQuery);
        return CONVERTER.permission2PermissionVoList(permissionList);
    }

    @Override
    public PermissionVO add(PermissionDTO dto) {
        Permission permission = CONVERTER.permissionDto2Permission(dto);
        permissionMapper.insert(permission);
        return CONVERTER.permission2PermissionVo(permission);
    }

    @Override
    public boolean delete(PermissionDeleteDTO dto) {
        LambdaQueryWrapper<Permission> permissionQuery = new LambdaQueryWrapper<Permission>()
                .in(CollectionUtils.isNotEmpty(dto.getIds()), Permission::getId, dto.getIds())
                .in(CollectionUtils.isNotEmpty(dto.getCodes()), Permission::getCode, dto.getCodes());
        int delete = permissionMapper.delete(permissionQuery);
        return delete > 0;
    }

    @Override
    public boolean update(PermissionUpdateDTO dto) {
        Permission permission = CONVERTER.permissionUpdateDTO2Permission(dto);
        int update = permissionMapper.updateById(permission);
        return update > 0;
    }

}
